﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for SanPhamDAO
/// </summary>
namespace Web_cuiki
{

    public class SanPhamDAO
    {
        public SanPhamDAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static List<SAN_PHAM> laydsAllSanPham()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<SAN_PHAM> ds = null;
            var query = from sp in db.SAN_PHAMs
                        select sp;
            ds = query.ToList<SAN_PHAM>();
            return ds;
        }
        public static int LaySoLuongSanPham(string ma)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            int maSP = Int32.Parse(ma);
            var query = from sp in db.SAN_PHAMs where sp.MaSanPham == maSP select sp;
            return (int)query.First().SoLuongTon;
        }
        public static List<SAN_PHAM> LayDSTheoMaTheLoai(string maTheLoai)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<SAN_PHAM> ds = null;
            int maTL = Int32.Parse(maTheLoai);
            var query = from sp in db.SAN_PHAMs where sp.MaTheLoai == maTL select sp;
            ds = query.ToList<SAN_PHAM>();
            return ds;
        }
        public static List<SAN_PHAM> LayDSTheoMaKieu(string maKieu)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<SAN_PHAM> ds = null;
            int maK = Int32.Parse(maKieu);
            var query = from sp in db.SAN_PHAMs where sp.MaKieuSanPham == maK select sp;
            ds = query.ToList<SAN_PHAM>();
            return ds;
        }
        public static List<SAN_PHAM> LayDSSanPhamMoi()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<SAN_PHAM> ds = null;
            var query = (from sp in db.SAN_PHAMs orderby sp.NgayPhatHanh select sp).Take(20);
            ds = query.ToList<SAN_PHAM>();
            return ds;
        }
        public static List<SAN_PHAM> TimSanPhamTheoTen(string tenSp)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<SAN_PHAM> ds = null;
            var query = from sp in db.SAN_PHAMs where sp.TenSanPham == tenSp select sp;
            ds = query.ToList<SAN_PHAM>();
            return ds;
        }
        public static int ThemSanPham(string tenTaiLieu, int maKieuSanPham, int maTheLoai, int maTacGia, string moTa, DateTime ngayPhatHang, int soLuongTon, double gia, int maKhuyenMai,string hAnh)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<SAN_PHAM> table = db.GetTable<SAN_PHAM>();
            if (hAnh == null || hAnh.Equals(""))
            {
                hAnh = "images/thumb1.gif";
            }
            try
            {
                SAN_PHAM sp = new SAN_PHAM();
                sp.Gia = gia;
                sp.MaKhuyenMai = maKhuyenMai;
                sp.MaKieuSanPham = maKieuSanPham;
                sp.MaTacGia = maTacGia;
                sp.MaTheLoai = maTheLoai;
                sp.MoTa = moTa;
                sp.TenSanPham = tenTaiLieu;
                sp.SoLuongTon = soLuongTon;
                sp.NgayPhatHanh = ngayPhatHang;
                sp.HinhAnh = hAnh;
                table.InsertOnSubmit(sp);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return kq;

        }
        public static int SuaSanPhamTheoMa(int maSP, string tenTaiLieu, int maKieuSanPham, int maTheLoai, int maTacGia, string moTa, DateTime ngayPhatHang, int soLuongTon, double gia, int maKhuyenMai)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<SAN_PHAM> table = db.GetTable<SAN_PHAM>();
            var query = from sph in db.SAN_PHAMs where sph.MaSanPham == maSP select sph;
            SAN_PHAM sp = query.First<SAN_PHAM>();
            try
            {
                if (gia != null)
                    sp.Gia = gia;
                if (maKhuyenMai != null)
                    sp.MaKhuyenMai = maKhuyenMai;
                if (maKieuSanPham != null)
                    sp.MaKieuSanPham = maKieuSanPham;
                if (maTacGia != null)
                    sp.MaTacGia = maTacGia;
                if (maTheLoai != null)
                    sp.MaTheLoai = maTheLoai;
                if (moTa != null)
                    sp.MoTa = moTa;
                if (tenTaiLieu != null)
                    sp.TenSanPham = tenTaiLieu;
                if (soLuongTon != null)
                    sp.SoLuongTon = soLuongTon;
                if (ngayPhatHang != null)
                    sp.NgayPhatHanh = ngayPhatHang;

                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;

        }
        public static int ThemHinhSanPham(int maSP, string hinh)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<SAN_PHAM> table = db.GetTable<SAN_PHAM>();
            var query = from sph in db.SAN_PHAMs where sph.MaSanPham == maSP select sph;
            SAN_PHAM sp = query.First<SAN_PHAM>();
            try
            {
                if (hinh != null)
                    sp.HinhAnh = hinh;
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static int XoaTheoMa(int maKM)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            try
            {
                var query = from hd in db.SAN_PHAMs where hd.MaSanPham == maKM select hd;
                SAN_PHAM hoadon = query.First<SAN_PHAM>();
                System.Data.Linq.Table<SAN_PHAM> table = db.GetTable<SAN_PHAM>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }

        public static SAN_PHAM LaySanPhamTheoMa(string maSp)
        {
            SAN_PHAM rs = null;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            if (maSp != null && maSp != "")
            {
                int ma = Int16.Parse(maSp);
                var query = from sp in db.SAN_PHAMs
                            where sp.MaSanPham == ma
                            select sp;
                rs = query.FirstOrDefault();
            }
            return rs;
        }

    }
}